package com.ship.dispatch.service.fob.impl;

import com.ship.common.business.service.BaseService;
import com.ship.dispatch.annotation.DataChangeLog;
import com.ship.dispatch.bpm.utils.JsonUtils;
import com.ship.dispatch.service.fob.FobBaseService;
import com.ship.dispatch.service.fob.FobService;
import com.ship.dispatch.service.fob.ModulContentParser;
import com.ship.dispatch.service.fob.SpFobZzgcjrService;
import com.ship.dispatch.util.ReflectionUtils;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;

/**
 *@title ModulContentParserImpl
 *@description
 *@author yl
 *@create 2023/12/13 0013
 */
@Component
public class ModulContentParserImpl implements ModulContentParser {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    FobService fobService;
    @Override
    public Object getResult(JoinPoint joinPoint, DataChangeLog operateLog) {
        Object info = joinPoint.getArgs()[1];
        Object data = ReflectionUtils.getFieldValue(info, "data");
        Object id = null;
        if(data!=null){
            Map<String,String> map = JsonUtils.jsonToMap(JsonUtils.objectToJson(data),String.class);
            if(map.containsKey("id")){
                id = map.get("id");
            }
        }else {
            id = ReflectionUtils.getFieldValue(info, "id");
        }
        if(id==null){
            return null;
        }
        Map<String,String> node = JsonUtils.jsonToMap(JsonUtils.objectToJson(info),String.class);
        Class baseClass = fobService.getServiceClass(node.get("key"));
        FobBaseService service = (FobBaseService) applicationContext.getBean(baseClass);
        Object result = service.getById((Serializable) id);
        return result;
    }
}
